package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.pojo.Order;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.pojo.dto.OrderDTO;
import com.start_eternity.oasissafewalk.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * author:ZhangT
 * Date:2024 / 01 / 31 / 21:32
 * Description:
 */
@Slf4j
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private OrderService orderService;


    //分页查询
    @GetMapping("/page")
    public Result<PageBean> findPages(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10")Integer pageSize) {
        PageBean pageBean = orderService.queryPage(page, pageSize);
        if (pageBean != null) {
            return ResultUtils.success(pageBean);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //根据orderNo 链表查询订单详情
    @GetMapping("/details")
    public Result<Order> selectDetailsByNo(@RequestParam String no) {
        Order order = orderService.getDetailsByNo(no);
        if (order != null) {
            return ResultUtils.success(order);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //根据orderId查询订单
    @GetMapping("/checkId")
    public Result<Order> checkById(@RequestParam Integer id) {
        Order order = orderService.queryOrderById(id);
        if (order != null) {
            return ResultUtils.success(order);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //根据orderNo 链表查询父母详情
    @GetMapping("/parent")
    public Result<Order> selectParentByNo(@RequestParam String no) {
        Order order = orderService.getParentByNo(no);
        if (order != null) {
            return ResultUtils.success(order);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }
    //根据orderNo查询订单
    @GetMapping("/checkNo")
    public Result<Order> checkByNo(@RequestParam String no) {
        Order order = orderService.queryOrderByNo(no);
        if (order != null) {
            return ResultUtils.success(order);
        }
        return ResultUtils.error(ErrorCode.NO_DATA);
    }

    //添加订单
    @PostMapping("/save")
    public Result<Integer> saveOrder(@RequestBody Order order) {
        log.info("新增订单,order:{}", order);
        int i = orderService.addOrder(order);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_INSERT);
        }
        return ResultUtils.success(i);
    }

    //根据orderId修改订单
    @PostMapping("/updateById")
    public Result<Integer> updateById(@RequestBody Order order){
        log.info("更新订单信息:{}",order);
        int i = orderService.changeOrderById(order);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_UPDATE);
        }
        return ResultUtils.success(i);
    }

    //根据orderNo修改订单
    @PostMapping("/updateByNo")
    public Result<Integer> updateByNo(@RequestBody Order order){
        log.info("更新订单信息:{}",order);
        int i = orderService.changeOrderByNo(order);
        if (i == 0) {
            return ResultUtils.error(ErrorCode.FAIL_UPDATE);
        }
        return ResultUtils.success(i);
    }

    //更具orderNo获取订单必要信息
    @GetMapping("/getOrderByNo")
    public Result<OrderDTO> getOrderByNo(@RequestParam String orderNo) {
        OrderDTO order = orderService.getOrder(orderNo);
        if (order == null) {
            return ResultUtils.error(ErrorCode.NO_DATA);
        }
        return ResultUtils.success(order);
    }

}
